<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <div class="app-space-between">
        <div class="app-space-between-left">
          <el-form-item label="文章标题">
            <el-input
              v-model="queryParams.title"
              placeholder="请输入文章标题"
              clearable
              @clear="handleQuery"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="文章专区">
            <el-input
              v-model="queryParams.articleRegion"
              placeholder="请输入文章专区"
              clearable
              @clear="handleQuery"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="审核状态">
            <el-select v-model="queryParams.available" @change="handleQuery" placeholder="审核状态" clearable>
              <el-option
                v-for="dict in optionsDict"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>

          <el-form-item label="栏目分类">
              <el-cascader
                v-model="queryParams.categoryId"
                :options="dataTable"
                filterable
                :props="{
                  value: 'categoryId',
                  label: 'categoryName'
                }"
                clearable
                @change="handleQuery"
                ></el-cascader>
          </el-form-item>
        </div>
        <div class="app-space-between-right">
          <el-form-item>
            <el-button type="primary" size="mini" @click="handleQuery">搜索</el-button>
            <el-button size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </div>
      </div>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:post:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          size="mini"
          :disabled="single"
          @click="handleUpdate(null)"
          v-hasPermi="['system:post:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:post:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
		  @click="handleSend"
		  :disabled="single"
          size="mini"
          v-hasPermi="['system:post:export']"
        >批量发布</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="postList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="文章标题" align="center" prop="title" />
      <el-table-column label="文章模型" align="center" prop="articleModel" />
	  <el-table-column label="文章专区" align="center" prop="articleRegion" />
      <el-table-column label="动态页面" align="center" width="80" prop="templateName" />
      <el-table-column label="标签" align="center" prop="tags" />
      <el-table-column label="点击数" align="center" width="80" prop="hit " />
      <el-table-column label="状态" align="center" width="80">
      <template slot-scope="scope">
        <el-tag>{{scope.row.available | $availableFilter}}</el-tag>
      </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
			@click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            v-if="scope.row.available != 1"
            @click="handleSend(scope.row)"
          >发布</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加和编辑 -->
    <add-and-edit ref="add-and-edit" @submit-success="getList"></add-and-edit>
  </div>
</template>

<script>
import { articleFind,articleRemove,articleDetail,articleBatchPublish } from "@/api/education/article";
import { categoryFindList } from '@/api/education/column';
import addAndEdit from "./components/addAndEdit.vue";
export default {
  name: "Article",
  dicts: ['sys_normal_disable'],
  components: {
    addAndEdit
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 岗位表格数据
      postList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: '',
        articleRegion: '',
        available: '',
        categoryId: ''
      },
      dataTable:[],
	  optionsDict:[
		  {value:0,label:'草稿'},
		  {value:1,label:'发布'},
		  {value:2,label:'待审核'},
	  ]
      // 表单参数
    };
  },
  created() {
    this.getList();
    categoryFindList({}).then(res=>{
    	this.dataTable = this.handleTree(res.data, "categoryId");
    })
  },
  methods: {
	/** 批量发布 **/
	 handleSend(options){
     let ids = ''
     if(options.id) ids = `${options.id}`
     else ids = this.ids.toString()
		 articleBatchPublish(ids).then(res=>{
			 console.log('发布成功',res)
			 this.getList();
		 })
	 },
	/** 查询岗位列表 */
    getList() {
      this.loading = true;
      let {categoryId} = this.queryParams
      if(Array.isArray(categoryId) && categoryId.length > 0) categoryId = categoryId[categoryId.length - 1]
	  articleFind({...this.queryParams,categoryId}).then(res=>{
		  this.postList = res.rows
		  this.total = res.total;
		  this.loading = false;
	  })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
		this.queryParams.pageNum = 1
		this.queryParams.pageSize = 10
		this.queryParams.title = ''
		this.queryParams.articleRegion = ''
		this.queryParams.available = ''
		this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
	  this.selectionAll = selection
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$refs['add-and-edit'].init(1)
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
		let id;
		if(row) id = row.id
		else id = this.selectionAll[0].id
	  articleDetail(id).then(res=>{
		 this.$refs['add-and-edit'].init(2,res.data)
	  })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
	  if(row.id) row.id = `${row.id}`;
      const data = {
		  ids: row.id || this.ids.toString()
	  };
      this.$modal.confirm('是否确认删除？').then(function() {
        return articleRemove(data.ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/post/export', {
        ...this.queryParams
      }, `post_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
